package com.fileshare.web.controller.index.view;

import com.fileshare.web.controller.index.BaseController;
import com.fileshare.web.dto.Vo.AllArticleCommentVo;
import com.fileshare.web.dto.Vo.ArticleVo;
import com.fileshare.web.dto.info.WebInfo;
import com.fileshare.web.service.ArticleService;
import com.fileshare.web.service.CommentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.fileshare.web.constant.EntityConstant.ARTICLE_REFERENCE_TYPE;

/**
 * 文章
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {
    @Resource
    private ArticleService articleService;
    @Resource
    private CommentService commentService;

    @GetMapping(value = {"/view/{articleSeq}"})
    public String displayArticle(HttpServletRequest request, @PathVariable String articleSeq) {
        ArticleVo articleVo = articleService.getArticle(articleSeq);
        WebInfo info = new WebInfo();
        info.setTitle(articleVo.getTitle());
        if (Boolean.TRUE.equals(articleVo.getAllowComment())) {
            List<AllArticleCommentVo> commentVos = commentService.searchCommentByArticleSeq(articleSeq, ARTICLE_REFERENCE_TYPE);
            request.setAttribute("commentVos", commentVos);
        }
        articleVo.setLabels(articleService.getLabels(articleSeq));
        request.setAttribute("webInfo", info);
        request.setAttribute("article", articleVo);
        return view("article/preview");
    }

    @GetMapping("/resource")
    @PreAuthorize("@auth.matchAny('articlePublisher:create')")
    public String resource() {
        return view("article/resource-publish");
    }

    @GetMapping("/rich_text")
    public String article_t() {
        return view("article/article-rich-text");
    }

    @GetMapping("/normal")
    @PreAuthorize("@auth.matchAny('articlePublisher:create')")
    public String normal() {
        return view("article/article-publish");
    }
}
